import {
    Http
} from "../utils/http.js";

class Categories {
    allArr = []
    zindex = -1
    currentIndex
    onSHow = true
    async getAll(shopid) {
        const res = await Http.request({
            url: `/main/get_menu_all`,
            data: {
                shopid
            }
        })
        this.allArr = res.data
        return this.allArr
    }

    /**
     * 查找当前激活root
     * 本地id查找 没有选第一项
     */
    getCurrentRoot(localId) {
        const currentRoot = this.allArr.find(item => {
            item == localId
        })
        if (!currentRoot) {
            return this.allArr[0]
        }
        return currentRoot
    }

    /**
     * 加减商品 商品状态改变
     */
    upDataCategory(carts) {
        const allArr = this.allArr
        allArr.forEach(fooMenu => {
            let i = 0;
            fooMenu['num'] = 0
            fooMenu.list.forEach(foodItem => {
                foodItem['num'] = 0
                carts.forEach(cartItem => {
                    if (foodItem.id == cartItem.gid) {
                        foodItem["num"] = cartItem.num;
                        i += cartItem.num;
                    }
                });
            });
            fooMenu["num"] = i;
        });
        return allArr
    }

    // 获取二级分类每个分类的高度
    getSubsItemHeight(cb) {
        let heightArr = []
        let start = 0
        let end = 0
        let h = 0
        const query = wx.createSelectorQuery()
        query.selectAll('.subs-item-container').boundingClientRect((res) => {
            // 第一种数据结构[[start,end]]
            res.forEach(item => {
                start = end
                end = start + item.height
                let range = [start, end]
                heightArr.push(range)
            })
            cb(heightArr)

            return
            // 第二中数据结构 [1,2,3]
            res.forEach(item => {
                h += item.height
                heightArr.push(h)
            })


        }).exec(res => {})
    }

    findCurrentRootsId(currentTop, arr, cb) {
        // 对应arr[[start,end]]的数据结构  zindex 初始值为负值
        for (let i = 0; i < arr.length; i++) {
            if (currentTop >= arr[i][0] && currentTop <= arr[i][1]) {
                if (i != this.zindex) {
                    this.zindex = i
                    cb(i)
                }
            }
        }

        return

        // 对应arr[1,2,3]的数据结构 zin dex初始值为0
        for (let i = 0; i < arr.length; i++) {
            if (currentTop >= 0 && currentTop < arr[0]) { // 第一类
                if (this.onSHow) {
                    this.onSHow = false
                    this.zindex = 0
                    cb(0)
                }
                return
            }

            if (currentTop >= (arr[i - 1]) && currentTop < arr[i]) { // 第i类
                if (i != this.zindex) {
                    this.onSHow = true
                    this.zindex = i
                    cb(i)
                }
            }
        }
    }


}
export {
    Categories
}